<?php
class Student {
    private $surname;
    private $firstName;
    private $emails;
    private $grades; // Now stores array of [course => Course, grade => float]
    private $status;
    
    public function __construct($surname, $firstName, $status = "") {
        $this->surname = $surname;
        $this->firstName = $firstName;
        $this->emails = array();
        $this->grades = array();
        $this->status = $status;
    }
    
    public function setStatus($status) {
        $this->status = $status;
    }
    
    public function add_email($type, $address) {
        $this->emails[$type] = $address;
    }
    
    public function add_grade(Course $course, $grade) {
        $this->grades[] = array(
            'course' => $course,
            'grade' => $grade
        );
    }
    
    public function average() {
        if (empty($this->grades)) return 0;
        
        $total = 0;
        foreach ($this->grades as $entry) {
            $total += $entry['grade'];
        }
        return number_format($total / count($this->grades), 2);
    }
    
    public function weightedAverage() {
        if (empty($this->grades)) return 0;
        
        $totalPoints = 0;
        $totalCredits = 0;
        
        foreach ($this->grades as $entry) {
            $totalPoints += $entry['grade'] * $entry['course']->getCredits();
            $totalCredits += $entry['course']->getCredits();
        }
        
        return $totalCredits > 0 ? number_format($totalPoints / $totalCredits, 2) : 0;
    }
    
    public function toString() {
        $result = $this->firstName . ' ' . $this->surname . " (Status: " . $this->status . ")\n";
        $result .= "  Average Grade: " . $this->average() . "\n";
        $result .= "  Weighted Average: " . $this->weightedAverage() . "\n";
        $result .= "  Courses:\n";
        
        foreach ($this->grades as $entry) {
            $result .= "    - " . $entry['course']->toString() . ": " . $entry['grade'] . "\n";
        }
        
        $result .= "  Emails:\n";
        foreach ($this->emails as $type => $address) {
            $result .= "    - " . ucfirst($type) . ": " . $address . "\n";
        }
        
        return "<pre>" . $result . "</pre>";
    }
}
?>